<!--
 * @Description: 选择品名
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-04-11 21:45:15
 * @FilePath: /yuxiuproweb/src/views/mes/pro/workorder/mods/add/part.vue
-->

<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="产品搜索"
    :visible.sync="shows"
    width="1060px"
    append-to-body
  >
    <div class="part">
      <div class="part-left">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item label="品名" prop="workorderCode">
            <el-input
              v-model="paramsName"
              placeholder="请输入品名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <div style="margin-bottom: 40px">
          <el-table ref="multipleTable" :data="curtainList" :height="175">
            <el-table-column width="60" align="center">
              <template slot-scope="{ row }">
                <el-radio
                  v-model="row.checked"
                  class="radio-view"
                  :label="true"
                  @change="checkedChange(row, 'curtainList')"
                  ><i></i
                ></el-radio>
              </template>
            </el-table-column>
            <el-table-column label="品名" prop="name">
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  @click="setImage(row), checkedChange(row, 'curtainList')"
                  >{{ row.name }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column
              label="相关信息"
              prop="processInfo"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="wholesalePrice"
              width="80"
            ></el-table-column>
          </el-table>
          <pagination
            v-show="curtainTotal > 0"
            :total="curtainTotal"
            :page.sync="curtainQueryParams.pageNum"
            :limit.sync="curtainQueryParams.pageSize"
            @pagination="getCurtainList"
          />
        </div>

        <div>
          <el-table
            class="mt10"
            ref="multipleTable"
            :data="gdCurtainList"
            :height="175"
          >
            <el-table-column width="60" align="center">
              <template slot-scope="{ row }">
                <el-radio
                  v-model="row.checked"
                  class="radio-view"
                  :label="true"
                  @change="checkedChange(row, 'gdCurtainList')"
                  ><i></i
                ></el-radio>
              </template>
            </el-table-column>
            <el-table-column label="品名" prop="name">
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  @click="setImage(row), checkedChange(row, 'gdCurtainList')"
                  >{{ row.name }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column
              label="相关信息"
              prop="processInfo"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="wholesalePrice"
              width="80"
            ></el-table-column>
          </el-table>
          <pagination
            v-show="gdTotal > 0"
            :total="gdTotal"
            :page.sync="gdQueryParams.pageNum"
            :limit.sync="gdQueryParams.pageSize"
            @pagination="getGdCurtainList"
          />
        </div>
      </div>
      <div class="part-right ml10">
        <el-image :src="skuImg">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
    <!-- 配置高端定制 -->
    <detail-view
      v-if="detail.show"
      :show.sync="detail.show"
      v-bind="detail"
      @on-change="handleDetailChange"
    ></detail-view>
  </el-dialog>
</template>

<script>
import { listCurtain, getCurtaingetInfo } from "@/api/goods/curtain";
import { listCustomization } from "@/api/goods/gd";
import DetailView from "./detail.vue";
export default {
  components: {
    DetailView,
  },
  props: {
    show: Boolean,
    type: {
      type: String,
      default: "add",
    },
    data: {
      type: Object,
    },
  },
  data() {
    return {
      paramsName: "",
      // 查询参数
      curtainQueryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
      },
      gdQueryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
      },
      detail: {
        show: false,
        data: null,
      },
      curtainTotal: 0,
      gdTotal: 0,
      curtainList: [],
      gdCurtainList: [],
      multipleSelection: [],
      queryParams: {},
      skuImg: "",
      checkedRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getCurtainList();
    this.getGdCurtainList();
  },
  methods: {
    getCurtainList() {
      listCurtain(this.curtainQueryParams).then(({ rows, total }) => {
        this.curtainList = rows?.map((e) => ({
          ...e,
          id: e.customCurtainId,
          checked: this.checkedRow?.id === e.customCurtainId,
          types: "curtain",
        }));
        this.curtainTotal = total;
      });
    },
    getGdCurtainList() {
      listCustomization(this.gdQueryParams).then(({ rows, total }) => {
        this.gdCurtainList = rows?.map((e) => ({
          ...e,
          id: e.highEndCustomizationId,
          checked: this.checkedRow?.id === e.highEndCustomizationId,
          types: "gd",
        }));
        this.gdTotal = total;
      });
    },

    handleQuery() {
      this.curtainQueryParams.name = this.paramsName;
      this.gdQueryParams.name = this.paramsName;
      this.getCurtainList();
      this.getGdCurtainList();
    },
    resetQuery() {
      this.paramsName = "";
      this.handleQuery();
    },
    /** 显示右侧图片 */
    setImage({ skuPicture }) {
      this.skuImg = skuPicture;
    },
    checkedChange(row, key) {
      this[key]?.forEach((e) => {
        if (e.id !== row.id) e.checked = false;
        else e.checked = true;
      });
      if (key !== "curtainList") {
        this.curtainList?.forEach((e) => (e.checked = false));
      } else {
        this.gdCurtainList?.forEach((e) => (e.checked = false));
      }

      this.checkedRow = {
        ...row,
      };
    },
    handleDetailChange(data) {
      const item = data?.find((e) => e.part === "-1" && e.part2 === "-1");
      this.$emit(
        "on-change",
        {
          ...this.detail.data,
          wholesalePrice: item?.wholesalePrice1 || 0,
          ...(item?.direction
            ? {
                direction: item?.direction,
              }
            : {}),
        },
        data
      );
      this.shows = false;
    },

    /** 获取定制窗帘材料 */
    async getCurtaingetInfo(id) {
      const res = await getCurtaingetInfo(id);

      let list3 = [];
      (res.data.complexMachiningList || []).forEach((e) => {
        if (e.isStandardConfig === "Y") {
          (e.complexMachining?.complexMachiningFabricList || []).forEach(
            (v) => {
              list3.push({
                direction: "",
                fabricId: v.fabricId,
                larghezza: v.width,
                name: v?.fabricName,
                number: "",
                part: v.part,
                source: "",
                part2: v.type,
                type: v.type,
                unit: "",
                wholesalePrice1: v?.wholesalePrice || null,
              });
            }
          );
        }
      });

      const fabricList = [
        {
          direction: res.data?.widthOrHeight,
          fabricId: res.data.fabric?.mdFabricId,
          larghezza: res.data.fabric?.width,
          name: res.data.fabric?.name,
          number: "",
          part: "-1",
          source: "",
          part2: "-1",
          type: "-1",
          unit: "",
          wholesalePrice1: res.data.fabric?.wholesalePrice1,
        },
        // ...list2,
        ...list3,
      ];

      const item = res.data.productType5List?.find(
        (e) => e.isStandardConfig === "Y"
      );
      const straps = {
        orderCustomCurtainStrapsId: "",
        name: item.otherMaterial.name,
        info: "",
        price: "",
        orderCustomCurtainId: "",
        highEndCustomizationId: "",
        otherMaterialId: item.otherMaterial.otherMaterialId,
        quantity: "",
        productFabrics: [
          {
            direction: res.data?.widthOrHeight,
            fabricId: res.data.fabric?.mdFabricId,
            larghezza: res.data.fabric?.width,
            name: res.data.fabric?.name,
            number: "",
            part: item.otherMaterial.name,
            source: "",
            part2: "-2",
            type: "-2",
            unit: "",
            wholesalePrice1: res.data.fabric?.wholesalePrice1,
          },
        ],
      };

      return {
        straps,
        fabricList,
      };
    },

    /** 提交按钮 */
    async submitForm() {
      switch (this.checkedRow?.types) {
        case "gd":
          this.detail = {
            show: true,
            data: {
              ...this.checkedRow,
            },
          };
          break;
        case "curtain":
          const { straps, fabricList } = await this.getCurtaingetInfo(
            this.checkedRow.id
          );
          const item = fabricList?.find(
            (e) => e.part === "-1" && e.part2 === "-1"
          );
          this.$emit(
            "on-change",
            {
              ...this.checkedRow,
              ...(item?.direction
                ? {
                    direction: item?.direction,
                  }
                : {}),
            },
            fabricList,
            straps
          );
          this.shows = false;
          break;
        default:
          break;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.part {
  width: 100%;
  display: flex;
  &-left {
    width: 600px;
  }
  &-right {
    flex: 1;
    border: 1px solid rgba(220, 220, 220, 0.5);
    border-radius: 4px;
    height: 422px;
    padding: 10px;
    img {
      width: 400px;
      height: 400px;
    }
    ::v-deep {
      .image-slot {
        width: 400px;
        height: 400px;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #909399;
      }
      .el-image {
        width: 100%;
        height: 100%;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>
